package yf.mall.service.impl;

import com.yf.response.RRException;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Propagation;
import yf.mall.dao.AddressDao;
import yf.mall.entity.AddressEntity;
import yf.mall.service.AddressService;
import com.yf.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 地址Service实现类
 *
 *
 *
 * @date Mon Aug 26 16:40:35 CST 2019
 */
@Service
@EnableAsync
public class AddressServiceImpl extends BaseServiceImpl<AddressEntity> implements AddressService {

    private AddressDao addressDao;


    @Autowired
    public void setDao(AddressDao addressDao) {
        this.addressDao = addressDao;
        super.setBaseDao(addressDao);
    }


    /**内部方法**/


    /**
     * 外部方法实现
     **/

    @Override
    public List<AddressEntity> findByUserId(Long userId) {
        Map params = new HashMap();
        params.put("userId", userId);
        params.put("order", "id desc");
        return addressDao.queryList(params);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AddressEntity merge(AddressEntity addressEntity) {
        validateAddress(addressEntity);
        if (addressEntity.getId() == null) {
            addressDao.save(addressEntity);
        } else {
            addressDao.update(addressEntity);
        }
        return addressEntity;
    }

    @Override
    public AddressEntity findDef(Long userId) {
        return addressDao.findDef(userId);
    }

    private void validateAddress(AddressEntity address) {
        if (address.getName() == null) {
            throw new RRException("请填写名字");
        }
        if (address.getMobile() == null) {
            throw new RRException("请填写手机号");
        }
        if (address.getMobile().length() < 11) {
            throw new RRException("请填写正确的手机号");
        }
        if (address.getProvince() == null) {
            throw new RRException("请填写省份!");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void test() {
        test1();
    }

    @Transactional(rollbackFor = Exception.class)
    public void test1() {
        AddressEntity address1 = new AddressEntity();
        address1.setName("1");
        save(address1);
        AddressEntity address2 = new AddressEntity();
        address2.setName("2");
        save(address2);
//        if(true){
//            throw new RRException("错误");
//        }
//        ttss();
    }

    @Resource
    @Lazy
    private AddressService self;

    void ttss() {
        self.test2();
    }

    @Async
    @Transactional(rollbackFor = Exception.class)
    public void test2() {

        try {
            Thread.sleep(1000 * 5);
        } catch (Exception e) {

        }
        AddressEntity address1 = new AddressEntity();
        address1.setName("3");
        save(address1);
        try {
            self.test3();
        } catch (Exception e) {

        }
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public void test3() {
        AddressEntity address2 = new AddressEntity();
        address2.setName("4");
        save(address2);
        throw new RRException("");
    }


}
